package com.plwf.lake.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plwf.lake.entity.ClassInfo;
import com.plwf.lake.service.ClassInfoService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程信息接口
 *@author :plwf
 *@time: 2019-5-12
 */
@RestController
@RequestMapping("/class")
public class ClassInfoCtrl {
    @Resource
    private ClassInfoService service;

    //查询所有课程信息包含： 分页 条件查询(subId:学员id，classSess:场次)
    @RequestMapping("/query")
    public Map<String,Object> query(HttpServletRequest request){
        //查询条件
        String subId = request.getParameter("subId");
        String classSess = request.getParameter("classSess");
        String stuId = request.getParameter("stuId"); //request.getParameter("stuId"); "11"
        Map<String,Object> map = new HashMap<>();
        if(subId!=null && !subId.equals("")){
            map.put("subId",Integer.parseInt(subId));
        }
        if(classSess!=null && !classSess.equals("")){
            map.put("classSess",classSess);
        }
        if(stuId!=null && !stuId.equals("")){
            map.put("stuId",stuId);
        }
        String coaId = request.getParameter("coaId");
        if(coaId!=null && !coaId.equals("")){
            map.put("coaId",Integer.parseInt(coaId));
        }

        //获取当前时间 作为条件查询今天及以后的所有预约信息
        Date teaTime = new Date();
        java.sql.Date sqlDate = new java.sql.Date(teaTime.getTime());
        map.put("teaTime",sqlDate);
        //设置分页条件
        int page=1;
        int rows=100;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }
        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        //查询出来的课程信息
        List<ClassInfo> list = service.query(map);
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);

        return rs;

    }
    //修改状态码
    @RequestMapping("enable/{classId}/{status}")
    public boolean enable(@PathVariable("classId") Integer classId, @PathVariable("status") Integer status) {
        return service.enable(classId, status);
    }
    @RequestMapping("/remove/{classId}")
    public boolean remove(@PathVariable("classId") Integer classId){
        return service.remove(classId);
    }

    @RequestMapping("/save")
    public ClassInfo save(ClassInfo bean){
        if(service.save(bean)){
            return bean;
        }else{
            return null;
        }

    }
    //查找单个课程信息
    @RequestMapping("/find/{classId}")
    public ClassInfo find(@PathVariable("classId") Integer classId){
        return service.find(classId);
    }

    //查找当前课程的预约人数，判断是否超过3人
    @RequestMapping("/findRes/{classId}")
    public ClassInfo findRes(@PathVariable("classId") Integer classId){
        return service.findRes(classId);
    }
    //查询该教练是否以及安排了今天的该场次的课程
    @RequestMapping("/queryClass")
    public boolean queryClass(ClassInfo bean){
        return this.service.findClass(bean);
    }
}
